home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Framework / Includes / UEventHandler.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  14.5 KB  |  399 lines  |  [TEXT/MPS ]

  1. // UEventHandler.h
  2. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4.  
  5. #ifndef __UEVENTHANDLER__
  6. #define __UEVENTHANDLER__
  7.  
  8. // MacApp
  9.  
  10. #ifndef __GEOMETRY__
  11. #include "Geometry.h"
  12. #endif
  13.  
  14. #ifndef __UITERATOR__
  15. #include "UIterator.h"
  16. #endif
  17.  
  18. #ifndef __UOBJECT__
  19. #include "UObject.h"
  20. #endif
  21.  
  22. // Toolbox
  23.  
  24.  
  25. //----------------------------------------------------------------------------------------
  26. // Some miscellaneous declarations.
  27. //----------------------------------------------------------------------------------------
  28.  
  29. enum IdlePhase { idleBegin, idleContinue, idleEnd };
  30.  
  31.  
  32. //----------------------------------------------------------------------------------------
  33. // Forward and external class declarations. 
  34. //----------------------------------------------------------------------------------------
  35.  
  36. class TBehavior;
  37. class TCommand;
  38. class TEvent;
  39. class TToolboxEvent;
  40. class TWindow;
  41.  
  42.  
  43. //----------------------------------------------------------------------------------------
  44. // TEventHandler: This type represents abstract objects that handle certain kinds of events:
  45. // Key events: both key down and auto key events, menu events: both enabling menus and
  46. // menu items and processing menu commands, Read to/ write from Disk, Idle events: when
  47. // there are no other events to handle, Actual events: EvtHandlers - through their method
  48. // 'DoHandleEvent' may intercept and handle actual ToolBox events Termination: when the
  49. // application quits TEventHandler are linked into a list with the most specific object
  50. // (usually a selection) at the head of the list. 'TApplication->fTarget' contains the head
  51. // of the list. (When a window is deactivated, this global variable is cached in the
  52. // window object, and retrieved when it is later activated.) For these kinds of events,
  53. // the TApplication->fTarget gets the first crack at handling the event. The default
  54. // implementation of the methods of TEventHandler is to pass the event to the next element
  55. // of the list.
  56. //----------------------------------------------------------------------------------------
  57.  
  58. class TEventHandler : public TObject
  59. {
  60.     MA_DECLARE_CLASS;
  61.     
  62.   public:
  63.     //------------------------------------------------------------------------------------
  64.     // Initializer, I<Method> and Free.
  65.     //------------------------------------------------------------------------------------
  66.  
  67.     TEventHandler();
  68.         // Constructor
  69.                 
  70.     void IEventHandler(TEventHandler* itsNextHandler);
  71.         // Call IObject and set fNextHandler to itsNextHandler.
  72.         
  73.     virtual ~TEventHandler();
  74.         // Before calling Inherited::Free, changes the target to the application if the
  75.         // next handler is NULL or the next handler if there is one.
  76.  
  77.     virtual TObject* Clone();
  78.         // Calls Inherited::Clone and then clones owned objects.
  79.  
  80.  
  81.     //------------------------------------------------------------------------------------
  82.     // Termination
  83.     //------------------------------------------------------------------------------------
  84.  
  85.     virtual void Terminate();
  86.         // Does nothing in this class.
  87.  
  88.  
  89.     //------------------------------------------------------------------------------------
  90.     // Stream I/O protocol support.
  91.     //------------------------------------------------------------------------------------
  92.  
  93.     virtual void ReadFrom(TStream* aStream);
  94.     
  95.     virtual void WriteTo(TStream* aStream);
  96.  
  97.  
  98.     //------------------------------------------------------------------------------------
  99.     // Chainwalking
  100.     //------------------------------------------------------------------------------------
  101.  
  102.     inline TEventHandler* GetNextHandler() const
  103.     { return fNextHandler; }
  104.         // Return the next handler in the chain
  105.  
  106.     TEventHandler* AddHandler(TEventHandler* headOfChain);
  107.         // Adds this from the chain of handlers starting at headOfChain. Returns the new
  108.         // head of chain.
  109.  
  110.     TEventHandler* RemoveHandler(TEventHandler* headOfChain);
  111.         // Removes this from the chain of handlers starting at headOfChain. Returns the
  112.         // new head of chain (if this was the head then there _must_ be a new head.
  113.  
  114.  
  115.     //------------------------------------------------------------------------------------
  116.     // Events
  117.     //------------------------------------------------------------------------------------
  118.  
  119.     long NextIdle();
  120.         // Returns the number Tickcount for when the handler should next be idled.
  121.  
  122.     void HandleIdle(IdlePhase phase);
  123.         // Calls DoIdle if the handler is eligible.
  124.  
  125.     virtual Boolean DoIdle(IdlePhase phase);
  126.         // Returns true if it frees itself so caller can know
  127.  
  128.     virtual Boolean DoCoHandlerEvent(TEvent* event);
  129.         // Handle the event and return true if I want it, else I don't handle it, and
  130.         // return false
  131.  
  132.     virtual void DoEvent(EventNumber eventNumber, TEventHandler* source, TEvent* event);
  133.         // By default, asks the next handler to handle the event
  134.  
  135.     virtual void HandleEvent(EventNumber eventNumber,
  136.                              TEventHandler* source,
  137.                              TEvent* event);
  138.         // Responsible for dispatching DoHandleEvent messages
  139.     
  140.     virtual void SetEnable(Boolean state);
  141.         // Enables based on 'state'
  142.  
  143.     inline Boolean IsEnabled() const
  144.     { return fEnabled; }
  145.         // Returns the enabled state.
  146.  
  147.  
  148.     //------------------------------------------------------------------------------------
  149.     // Double/ Triple Clicks
  150.     //------------------------------------------------------------------------------------
  151.  
  152.     virtual Boolean DoMultiClick(CPoint lastDownPt, CPoint newDownPt);
  153.         // Called by TApplication::CountClicks. Should return true if the 2 points are
  154.         // close enough to be considered part of a double/ triple click. (Both points are
  155.         // in global coordinates.) This is only called if the mouse down was within the
  156.         // proper time range of the previous mouse up. Default is to pass message to the
  157.         // nextEventHandler (if one exists), otherise to require that the sum of the x & y
  158.         // distances is <= gStdHysteresis.
  159.  
  160.  
  161.     //------------------------------------------------------------------------------------
  162.     // Key Events
  163.     //------------------------------------------------------------------------------------
  164.  
  165.     virtual void DoKeyEvent(TToolboxEvent* event);
  166.         // Handles all keystrokes except those with the command key held down.
  167.  
  168.     virtual void DoKeyUp(TToolboxEvent* event);
  169.         // Handles all key upstrokes.
  170.  
  171.     void HandleKeyCommand(TToolboxEvent* event);
  172.         // Handles the dispatching of DoKeyEvent messages between itself and Behaviors
  173.     
  174.     void HandleKeyUp(TToolboxEvent* event);
  175.         // Handles the dispatching of DoKeyUp messages between itself and Behaviors
  176.     
  177.     virtual void DoCommandKeyEvent(TToolboxEvent* event);
  178.         // Handles command-key combinations only.
  179.  
  180.     void HandleCommandKey(TToolboxEvent* event);
  181.         // Handles the dispatching of DoCommandKeyEvent messages between itself and Behaviors
  182.     
  183.     virtual void KeyEventToComponents(TToolboxEvent* event);
  184.         // Intended to extract character components of an event in a script manager
  185.         // compatible way.Default just forwards to next handler
  186.  
  187.  
  188.     //------------------------------------------------------------------------------------
  189.     // MenuEvents
  190.     //------------------------------------------------------------------------------------
  191.  
  192.     virtual void DoMenuCommand(CommandNumber aCommandNumber);
  193.         // If the handler can perform the command it does so by either performing the
  194.         // command directly or by posting a TCommand.
  195.  
  196.     void HandleMenuCommand(CommandNumber aCommandNumber);
  197.         // Handles the dispatching of DoMenuCommand messages
  198.     
  199.     virtual void DoSetupMenus();
  200.         // Handles the setup of menus.
  201.  
  202.     virtual void HandleSetupMenus();
  203.         // Handles the dispatching of DoSetupMenus messages
  204.     
  205.     //------------------------------------------------------------------------------------
  206.     // Help
  207.     //------------------------------------------------------------------------------------
  208.  
  209.     virtual void DoHelp(TToolboxEvent* event, long& message);
  210.         // If fNextHandler is not NULL calls its DoHelp method, otherwise returns NULL.
  211.  
  212.     //------------------------------------------------------------------------------------
  213.     // Target management
  214.     //------------------------------------------------------------------------------------
  215.  
  216.     virtual TWindow *GetWindow() const;
  217.         // returns the TWindow of this TEventHandler (if any). Returns NULL if this TEventHandler isn't a TView
  218.         // or isn't installed in a TWindow.
  219.         
  220.     virtual Boolean WantsToBeTarget();
  221.         // Called to find out if this object wishes to become the target. Default returns
  222.         // false
  223.  
  224.     virtual long WillingToResignTarget();
  225.         // Called to find out if this object is willing to cease being the target. Default
  226.         // returns true
  227.  
  228.     virtual void TargetValidationSucceeded();
  229.     
  230.     virtual void TargetValidationFailed(long reason);
  231.  
  232.     virtual void BecameWindowTarget();
  233.         // Called when this object becomes the current target of a window
  234.  
  235.     virtual void ResignedWindowTarget();
  236.         // Called when this object ceases to be the current target of a window
  237.  
  238.     virtual void BecameTarget();
  239.         // Called when this object becomes the current target
  240.  
  241.     virtual void ResignedTarget();
  242.         // Called when this object ceases to be the current target
  243.  
  244.     virtual Boolean BecomeTarget();
  245.         // Called to make this object be the current target
  246.  
  247.     virtual Boolean ResignTarget();
  248.         // Called to inform this object to cease being the current target
  249.  
  250.     virtual void SetTargetSelection(Boolean redraw);
  251.         // Called when this object becomes the target, except as a result of a mouse click
  252.         // Overridden by EditTexts to select all characters
  253.  
  254.     virtual Boolean IsTarget();
  255.         // Returns true when this object is the current target
  256.  
  257.  
  258.     //------------------------------------------------------------------------------------
  259.     // Miscellaneous
  260.     //------------------------------------------------------------------------------------
  261.  
  262.     virtual void DoKeySelection(const CStr255& selectionString);
  263.         // Derived classes override this method to set the selection based on the value of
  264.         // 'selectionString'. This method is called by a TKeySelectionBehavior which can
  265.         // be added to a TEventHandler and any of its derived classes.
  266.         
  267.     inline IDType GetIdentifier() const
  268.     { return fIdentifier; }
  269.         // Return the fIdentifier of the object.
  270.         
  271.     inline long GetIdleFreq() const
  272.     { return fIdleFreq; }
  273.         // Return the current value of fIdleFreq.
  274.         
  275.     virtual void SetIdleFreq(long newIdleFreq);
  276.         // Call to set the handler's idling frequency.
  277.  
  278.     void RemoveBehavior (TBehavior* theBehavior);
  279.         // Remove theBehavior from the behavior chain
  280.     
  281.     void AddBehavior (TBehavior* theBehavior);
  282.         // Call to add theBehavior to this object
  283.     
  284.     inline TBehavior* GetFirstBehavior() const
  285.     { return fBehavior; }
  286.         // Call to retrieve the first behavior currently attached to EvtHandler
  287.  
  288.     TBehavior* GetFirstEnabledBehavior () const;
  289.         // Call to retrieve the first enabled behavior currently attached to EvtHandler
  290.  
  291.     virtual TBehavior* GetBehaviorWithIdentifier(IDType itsIdentifier);
  292.         // Returns the first behavior with the specified ID, or NULL
  293.         
  294.     virtual void SelectOwner (Boolean select);
  295.         // "Selects" this object by calling the SelectOwner method of each behavior
  296.         
  297.     virtual Boolean IsSelected();
  298.         // Returns the value of the first behavior's IsOwnerSelected(), or false
  299.         // if there are no behaviors
  300.  
  301.     virtual void DoUpdate(ChangeID theChange,
  302.                           TObject* changedObject,
  303.                           TObject* changedBy,
  304.                           TDependencySpace* dependencySpace); // Override
  305.  
  306.  
  307.     //------------------------------------------------------------------------------------
  308.     // Command Management
  309.     //------------------------------------------------------------------------------------
  310.  
  311.     virtual TEvent* RetrieveAnEvent();
  312.         // Retrieves a previously posted event/ command from the queue. NULL if there are no
  313.         // queued commands. Default is to hand off to fNextHandler
  314.  
  315.     virtual void PostAnEvent(TEvent* event);
  316.         // Called to post an event or command to a queue for later processing. Default is
  317.         // to hand off to fNextHandler
  318.  
  319.     virtual void PostCommand(TCommand* command);
  320.         // Called to post a command to a queue for later execution. Left in for
  321.         // compatibility - calls PostAnEvent
  322.  
  323.     //------------------------------------------------------------------------------------
  324.     // data members
  325.     //------------------------------------------------------------------------------------
  326. public:
  327.  
  328.     TEventHandler* fNextHandler;                // the next element of the list, or NULL
  329.     
  330.     TBehavior*    fBehavior;                            // Behavioral modifier object
  331.     
  332.     IDType fIdentifier;                                    // the ID if any
  333.                                                 
  334.     long fIdleFreq;                                            // the minimum number of ticks that can
  335.                                                 // pass before my DoIdle is called. 0 =
  336.                                                 // call as often as possible.
  337.                                                 // n..kMaxIdleTime = call every fIdleFreq
  338.                                                 // ticks.
  339.                                                 
  340.     long fLastIdle;                                            // the tick count the last time my DoIdle
  341.                                                 // was called.
  342.  
  343.     Boolean fEnabled;                                        // is this eventHandler enabled. (does it
  344.                                                 // take clicks/keystrokes and other
  345.                                                 // events? 
  346.  
  347. //----------------------------------------------------------------------------------------
  348. // static data members
  349. //----------------------------------------------------------------------------------------
  350. public:
  351.  
  352.     static CPoint gStdHysteresis;                    // standard hysteresis used in TCommand::ICommand
  353. };
  354.  
  355.  
  356. //----------------------------------------------------------------------------------------
  357. // CHandlerIterator: A simple iterator for the handler chain.
  358. //----------------------------------------------------------------------------------------
  359.  
  360. class CHandlerIterator
  361. {
  362. public:
  363.     CHandlerIterator(TEventHandler* chainHead);
  364.     
  365.     inline Boolean More()                    // override
  366.     { return (fCurrentHandler != NULL); }
  367.         // Returns true if there are more elements to iterate over
  368.  
  369.     inline void Reset()                    // override
  370.     { fCurrentHandler = fFirstHandler; }
  371.         // Resets the iterator to begin again
  372.     
  373.     inline TEventHandler* CurrentHandler()
  374.     { return fCurrentHandler; }
  375.         // returns the current handler in the handler chain
  376.  
  377.     TEventHandler* FirstHandler();
  378.         // Resets the iterator to begin again and returns the first handler in the handler chain
  379.  
  380.     TEventHandler* NextHandler();
  381.         // returns the next handler in the handler chain
  382.     
  383. protected:
  384.     void Advance();                    // override
  385.         // Advances the iteration
  386.  
  387.     //------------------------------------------------------------------------------------
  388.     // data members
  389.     //------------------------------------------------------------------------------------
  390. private:
  391.     TEventHandler*    fFirstHandler;
  392.  
  393.     TEventHandler*    fCurrentHandler;
  394.  
  395.     TEventHandler*    fNextHandler;
  396. };
  397.  
  398. #endif
  399.